//
//  ShareEasyViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/1/9.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "ShareEasyViewController.h"
#import <UMSocialCore/UMSocialCore.h>


@interface ShareEasyViewController ()
{
    NSString *_imgUrl;
}

@end

@implementation ShareEasyViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分享可为建材联盟";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self shareQR_codeData];
}

- (void)shareQR_codeData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        NSDictionary *params = @{@"position":@"App_Weixin_Qr",};
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Ads/lists" completeBlock:^(id object) {
            
            NSLog(@"-----------%@",object);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (JudgeBackDataCode) {
                    NSArray *arr = [object objectForKey:@"data"];

                    _imgUrl = [arr[0] objectForKey:@"img"];
                    [self buildShareView];
                }
                
            });
            
        }];
        
    });
}




//微信分享 //qq分享
- (void)chatShareButtonClick:(UIButton *)sender
{
    
    switch (sender.tag) {
        case 1992:
            [self shareChatToPlatformType:UMSocialPlatformType_WechatSession];
            break;
            
        case 1993:
            [self shareChatToPlatformType:UMSocialPlatformType_QQ];
            break;
            
        default:
            break;
    }
    
   
}

- (void)shareChatToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = [UIImage imageNamed:@"shareEasy.jpg"];
    [shareObject setShareImage:_imgUrl];
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];

}


- (void)buildShareView
{
    UIImageView *shareImg = [[UIImageView alloc] init];
    [self.view addSubview:shareImg];
    shareImg.image = [UIImage imageNamed:@"shareEasy.jpg"];
    shareImg.frame = CGRectMake(25,30,WIDTH-50,WIDTH-50);
    
    UILabel *shareLbl = [[UILabel alloc] init];
    [self.view addSubview:shareLbl];
    shareLbl.textColor = [UIColor grayColor];
    shareLbl.text = @"点击下面的按钮,向好友分享可为联盟";
    shareLbl.textAlignment = NSTextAlignmentCenter;
    shareLbl.font = [UIFont boldSystemFontOfSize:18];
    [shareLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(shareImg.mas_centerX);
        make.top.equalTo(shareImg.mas_bottom).offset(20);
    }];
    
    UIButton *weChat = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:weChat];
    weChat.layer.masksToBounds = true;
    weChat.layer.cornerRadius = 30;
    weChat.tag = 1992;
    [weChat setImage:[UIImage imageNamed:@"icon_share_0"] forState:UIControlStateNormal];
    [weChat addTarget:self action:@selector(chatShareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [weChat mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shareLbl.mas_bottom).offset(20);
        make.right.equalTo(shareImg.mas_centerX).offset(-15);
        make.width.height.mas_equalTo(80);
    }];
    
    
    UIButton *qqChat = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:qqChat];
    qqChat.layer.masksToBounds = true;
    qqChat.layer.cornerRadius = 30;
    qqChat.tag = 1993;
    [qqChat setImage:[UIImage imageNamed:@"icon_share_1"] forState:UIControlStateNormal];
    [qqChat addTarget:self action:@selector(chatShareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [qqChat mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shareLbl.mas_bottom).offset(20);
        make.left.equalTo(shareImg.mas_centerX).offset(15);
        make.width.height.mas_equalTo(80);
    }];
    

}



@end
